package com.hhit.zhgd.controller

import cn.hutool.core.util.StrUtil
import com.hhit.zhgd.enums.ExcelHeaderTypeEnum
import jakarta.servlet.http.HttpServletResponse
import org.springframework.core.io.ClassPathResource
import org.springframework.core.io.Resource
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.io.BufferedInputStream
import java.io.BufferedOutputStream
import java.net.URLEncoder

@RestController
class ZhgdExcelTemplateController {
    @GetMapping("backStage/exportExcelTemplate")
    fun exportExcelTemplate(@RequestParam("templateName") templateName: String, response: HttpServletResponse) {
        var templatePath = StrUtil.EMPTY
        if (ExcelHeaderTypeEnum.SECURITY_CHECK.typeName == templateName) {
            templatePath = "templates/安全检查导入模板.xlsx"
        } else if (ExcelHeaderTypeEnum.PROJECT_INFO.typeName == templateName) {
            templatePath = "templates/工程信息模板.xlsx"
        } else if (ExcelHeaderTypeEnum.MILESTONE_PLAN.typeName == templateName) {
            templatePath = "templates/里程碑计划模板.xlsx"
        } else if (ExcelHeaderTypeEnum.TWO_PLAN.typeName == templateName) {
            templatePath = "templates/两个计划导入模板.xlsx"
        } else if (ExcelHeaderTypeEnum.HIGHLIGHT_GREEN_CONSTRUCTION.typeName == templateName) {
            templatePath = "templates/亮点模板.xlsx"
        } else if (ExcelHeaderTypeEnum.PERSON_INFO.typeName == templateName) {
            templatePath = "templates/人员信息导入模板.xlsx"
        } else if (ExcelHeaderTypeEnum.QUALITY_CHECK.typeName == templateName) {
            templatePath = "templates/质量检查导入模板.xlsx"
        } else if (ExcelHeaderTypeEnum.WORK_TICKETS.typeName == templateName) {
            templatePath = "templates/作业票模板.xlsx"
        } else if (ExcelHeaderTypeEnum.SHCDULE_INFO.typeName == templateName) {
            templatePath = "templates/进度横道图模板.xlsx"
        } else if (ExcelHeaderTypeEnum.CONSTRUCTION_KEYPOINTS.typeName == templateName) {
            templatePath = "templates/标准工艺模板.xlsx"
        } else if (ExcelHeaderTypeEnum.EQUIPMENT_MATERIAL_TESTING.typeName == templateName) {
            templatePath = "templates/设备材料检测计划.xlsx"
        } else if (ExcelHeaderTypeEnum.ZHGD_DRAWING_SUPPLY_PLAN.typeName == templateName) {
            templatePath = "templates/图纸供应计划.xlsx"
        } else if (ExcelHeaderTypeEnum.MATERIALS_SUPPLY_PLAN.typeName == templateName) {
            templatePath = "templates/物资供应计划.xlsx"
        } else if (ExcelHeaderTypeEnum.DEVICE_TEMPLATE.typeName == templateName) {
            templatePath = "templates/zhgd_device.xlsx"
        } else if (ExcelHeaderTypeEnum.QUALITY_ACCEPTANCE_PLAN.typeName == templateName) {
            templatePath = "templates/质量验收计划.xlsx"
        } else if (ExcelHeaderTypeEnum.ARCHIVE_SITUATION.typeName == templateName) {
            templatePath = "templates/归档目录.xlsx"
        } else {
            // 模板名称无效，返回错误信息或默认模板
        }
        try {
            // 加载模板文件
            val resource: Resource = ClassPathResource(templatePath)
            val inputStream = resource.inputStream
            val filename = "$templateName.xlsx"
            var encodedFilename = URLEncoder.encode(filename, "UTF-8")
            encodedFilename = encodedFilename.replace("+", "%20")
            response.contentType = "application/octet-stream"
            response.setHeader("Content-Disposition", "attachment; filename=\"$encodedFilename\"")
            BufferedInputStream(inputStream).use { `in` ->
                BufferedOutputStream(response.outputStream).use { out ->
                    val buffer = ByteArray(4096)
                    var bytesRead: Int
                    while (`in`.read(buffer).also { bytesRead = it } != -1) {
                        out.write(buffer, 0, bytesRead)
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
            //log.error("ZhgdExcelTemplateController.exportExcelTemplate Call Error", e)
            // 模板导出异常，返回错误信息
            // 创建文件响应对象
        }
    }

}